<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vxe-pc-ui/lib/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vxe-table/lib/style.css">
    <!-- 引入vue -->
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <!-- 引入组件 -->
    <script src="https://cdn.jsdelivr.net/npm/xe-utils"></script>
    <script src="https://cdn.jsdelivr.net/npm/vxe-pc-ui"></script>
    <script src="https://cdn.jsdelivr.net/npm/vxe-table"></script>
    <!-- 引入axios -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
        }

        .login-container {
            width: 500px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
        }

        .login-header {
            text-align: center;
            margin-bottom: 10px;
        }

        .login-header h2 {
            color: #409EFF;
        }
    </style>
</head>

<body>
<div id="app">
    <div class="login-container" style="margin-top: 100px;">
        <div class="login-header">
            <h2>{[{ .title }]}</h2></h2>
        </div>
        <vxe-form
                title-colon
                title-width="100"
                title-align="right"
                :data="formData"
                @submit="submitEvent"
                :rules="formRules">
            <vxe-form-item title="用户名" field="username" span="24" :item-render="{ name: 'VxeInput'}"
                           :title-prefix="{icon: 'vxe-icon-add-user' }"></vxe-form-item>
            <vxe-form-item title="密&nbsp;&nbsp;&nbsp;码" field="password" span="24"
                           :item-render="{ name: 'VxeInput', props: { type: 'password' }}"
                           :title-prefix="{icon: 'vxe-icon-add-user' }"></vxe-form-item>
            <vxe-form-item title="验证吗" field="captchaValue" span="10" :item-render="{ name: 'VxeInput'}"
                           :title-prefix="{icon: 'vxe-icon-signature' }"></vxe-form-item>
            <vxe-form-item>
                <template #default>
                    <img :src="captchaImage" @click="getList" style="width: 130px; height: 30px; cursor: pointer;">
                    <input type="hidden" value="captchaId">
                </template>
            </vxe-form-item>
            <vxe-form-item align="center" span="24">
                <template #default>
                    <vxe-button type="submit" status="primary">提交</vxe-button>
                    <vxe-button type="reset">重置</vxe-button>
                </template>
            </vxe-form-item>
        </vxe-form>
    </div>
</div>

<script>
    const {reactive, ref, createApp, onMounted} = Vue;
    const App = {
        setup() {
            const captchaImage = ref('');
            const captchaId = ref('');
            const formData = ref({
                username: '',
                password: '',
                captchaValue: ''
            })
            const formRules = reactive({
                username: [
                    {required: true, validator: 'ValidUserName'}
                ],
                password: [
                    {required: true, validator: 'ValidPassword'}
                ],
                captchaValue: [
                    {required: true, validator: 'ValidCaptcha'}
                ]
            })

            VxeUI.validators.add('ValidUserName', {
                formItemValidatorMethod({itemValue}) {
                    if (!itemValue || itemValue.trim() === '') {
                        return new Error('用户名不能为空！');
                    }
                }
            });
            VxeUI.validators.add('ValidPassword', {
                formItemValidatorMethod({itemValue}) {
                    if (!itemValue || itemValue.trim() === '') {
                        return new Error('密码不能为空！');
                    }
                }
            });
            VxeUI.validators.add('ValidCaptcha', {
                formItemValidatorMethod({itemValue}) {
                    if (!itemValue || itemValue.trim() === '') {
                        return new Error('验证码不能为空！');
                    }
                }
            });
            onMounted(() => {
                getList();
            });
            const submitEvent = () => {
                axios({
                    method: "post",
                    url: "admin/loginServer",
                    params: {
                        username: formData.value.username,
                        password: formData.value.password,
                        captchaValue: formData.value.captchaValue,
                        captchaId: captchaId.value
                    },
                }).then((res) => {
                    if (res.data.status === 0) {
                        window.location.href = "admin/index";
                        //VxeUI.modal.message({content: res.data.msg, status: 'success'})
                    } else {
                        VxeUI.modal.message({content: res.data.msg, status: 'error'})
                    }
                }).catch(function (err) {
                    console.log(err);
                });
            }
            const getList = () => {
                axios({
                    method: "get",
                    url: "admin/GetCaptcha",
                    params: {},
                }).then((res) => {
                    captchaImage.value = res.data.captchaImage;
                    captchaId.value = res.data.captchaId;
                }).catch(function (err) {
                    console.log(err);
                });
            };

            return {
                getList,
                formRules,
                formData,
                captchaImage,
                captchaId,
                submitEvent

            };
        },
    };
    createApp(App).use(VxeUI).use(VXETable).mount('#app')
</script>
</body>

</html>